<template>
  <div class="b1-page">
    <div class="tool-view" :style="{ width: tableW }">
      <div class="tool-l">
        <span>站点</span>
        <!-- <n-popselect
          v-model:value="params.modelId"
          :options="stationList"
          trigger="click"
        >
          <div>{{ params.modelI || "选择站点名称" }}</div>
        </n-popselect> -->
        <n-select
          placeholder="选择站点名称"
          style="width: 6.25rem"
          :consistent-menu-width="false"
          v-model:value="params.modelId"
          :options="stationList"
          @update:value="GetAllStationInfoFun"
        />
        <span>OP</span>
        <n-select
          placeholder="选择OP属性"
          style="width: 6.25rem"
          :consistent-menu-width="false"
          v-model:value="params.stationId"
          :options="OPList"
          @update:value="GetStationConfigurationFun"
        />
        <span>产品</span>
        <n-select
          placeholder="选择产品名称"
          style="width: 6.25rem"
          :consistent-menu-width="false"
          v-model:value="params.productName"
          :options="productNameList"
          @update:value="GetProductSNListFun"
        />
        <span>SN</span>
        <n-select
          placeholder="选择SN码"
          style="width: 6.25rem"
          :consistent-menu-width="false"
          v-model:value="params.productSN"
          :options="productSNList"
        />
        <span>时间</span>
        <div class="pcker-view">
          <div class="mask" @click="show = true"></div>
          <n-config-provider :locale="zhCN" :date-locale="dateZhCN">
            <n-date-picker
              style="width: 260px"
              v-model:value="range"
              type="daterange"
              :show="show"
              clearable
              :to="false"
              @update:formatted-value="timeChange"
              :is-date-disabled="disablePreviousDate"
              @confirm="show = false"
              @blur="show = false"
            />
          </n-config-provider>
        </div>
        <n-button
          style="margin-left: 12px"
          type="success"
          :disabled="!isQuery"
          @click="GetPageB1DataFun"
        >
          查询
        </n-button>
      </div>
    </div>
    <div class="table-view">
      <div v-if="stepsList.length > 0" class="table-left">
        <div class="title">执行日志</div>
        <n-scrollbar style="max-height: calc(100% - 1rem)">
          <n-steps vertical :current="stepsList.length" status="finish">
            <template #finish-icon>
              <icons class="icon" name="step" />
            </template>
            <n-step
              v-for="step in stepsList"
              :key="step.id"
              :title="step.remark"
            >
              <template #default>
                <!-- <div class="step-desc">
                  {{ step.remark }}
                </div> -->
                <div
                  v-for="(text, i) in step.partsName"
                  :key="i"
                  class="step-desc"
                >
                  {{ text }}
                </div>
              </template>
            </n-step>
          </n-steps>
        </n-scrollbar>
      </div>
      <n-scrollbar style="height: 100%">
        <div class="table-right">
          <div v-if="echarlsList.length === 0 && showSpin" class="load">
            <n-spin :show="showSpin">
              <template #description> 加载中 </template>
            </n-spin>
          </div>
          <div v-for="echart in echarlsList" :key="echart.id" class="card">
            <div class="title">{{ echart.propertyName }}</div>
            <div class="card-content">
              <Circular
                v-if="echart.chartType === '饼状图'"
                :data="echart.datas"
              />
              <Columnar
                v-else-if="echart.chartType === '柱状图'"
                :data="echart.datas"
              />
              <Polyline
                v-else-if="echart.chartType === '折线图'"
                :data="echart.datas"
              />
              <div v-else>{{ echart.chartType }}</div>
            </div>
          </div>
        </div>
      </n-scrollbar>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { today } from "@/tools/radom";
import Circular from "@/components/Echarts/Circular.vue"; //饼图
import Columnar from "@/components/Echarts/Columnar.vue"; //柱状图
import Polyline from "@/components/Echarts/Polyline.vue"; //折线
import {
  GetStationConfiguration,
  GetAllStationInfo,
  GetProductNameList,
  GetProductSNList,
  GetPageA2Name,
  GetAllProperty,
  PageB1DataBind,
  GetPageB1Data,
} from "@/server";
import { zhCN, dateZhCN } from "naive-ui";
import { useAppStore } from "@/store/modules/user";
import { mapStore } from "@/store/modules/map";
const route = useRoute();
// const modelId = computed(() => route.params.id);
const store = useAppStore();
const openMore = ref(false);
const stationList = ref([]) as any; //站台列表
const OPList = ref([]) as any; //站台列表
const params = ref({
  // modelId: "",
  // stationId: 0,
  // productName: "",
  // productSN: "",
  startTime: today(),
  endTime: today(),
}) as any;
const show = ref(false);
const showSpin = ref(false);
const stepsList = ref([]) as any; //站台列表
const productNameList = ref([]) as any; //产品名称列表;
const productSNList = ref([]) as any; //产品SN码列表
const echarlsList = ref([]) as any; //产品SN码列表
const propertyArr = ref([]) as any;
const range = ref<[number, number]>([Date.now(), Date.now()]);
const timeChange = (e: any) => {
  params.value.startTime = e[0];
  params.value.endTime = e[1];
};
const closeWin = () => {
  openMore.value = false;
};
const isQuery = computed(() => {
  const { modelId, stationId, productName, productSN, startTime, endTime } =
    params.value;
  return (
    modelId && stationId && productName && productSN && startTime && endTime
  );
});
//查询站台详情
const GetStationConfigurationFun = async () => {
  const res: any = await GetStationConfiguration({
    Id: params.value.stationId,
  });
  if (res.result.isSuccess) {
    stepsList.value = res.result.result.steps;
  } else {
    store.error(res.result.message); // 错误提示
  }
};
const onlyAllowNumber = (value: string) => {
  return !value || /^\d+$/.test(value);
};
const openMoreWin = () => {
  // modelType.value = type;
  let list = JSON.parse(JSON.stringify(propertyList.value));
  let list2: any = [];
  // if (type === "2-2") {
  //   list2 = A2List.value;
  // } else if (type === "2-3") {
  //   list2 = A3List.value;
  // } else if (type === "2-4") {
  //   list2 = A4List.value;
  // }
  list2.forEach((listItem: any) => {
    const [prefix, suffix] = listItem.propertyName.split("-");
    const matchingItem = list.find((item: any) => prefix === item.value);
    if (matchingItem) {
      const matchingData = matchingItem.datas.find(
        (data: any) => data.propertyName === suffix
      );
      if (matchingData) {
        matchingData.checked = true;
        matchingData.active = listItem.chartType;
      }
    }
  });
  list = list.map((m: any) => {
    m.datas = m.datas.map((e: any) => {
      if (!e.sort) {
        e.sort = 0 + "";
      } else {
        e.sort = e.sort + "";
      }
      return e;
    });
    return m;
  });
  propertyArr.value = list;
  openMore.value = true;
};
const tableW = ref("100%");
onMounted(() => {
  // 获取页面宽度
  const pageWidth = window.innerWidth;
  // 计算百分比
  const percentage = (pageWidth / (pageWidth * 0.7)) * 100;
  tableW.value = percentage + "%";
  console.log(tableW.value);
});
const lock = ref(false);
const unLock = () => {
  setTimeout(() => {
    lock.value = false;
  }, 1000);
};
//查询数据
const GetPageB1DataFun = async () => {
  if (lock.value || !isQuery.value) {
    return false;
  }
  showSpin.value = true;
  lock.value = true;
  const res: any = await GetPageB1Data(params.value);
  if (res.result.isSuccess) {
    echarlsList.value = res.result.result;
    if (echarlsList.value.length === 0) {
      store.info("未查询到数据"); // 错误提示
    }
    unLock();
    showSpin.value = false;
  } else {
    unLock();
    showSpin.value = false;
    store.error(res.result.message); // 错误提示
  }
};
const disablePreviousDate = (ts: number) => {
  return ts > Date.now();
};
const checkedFun = (bool: boolean, item: any) => {
  if (bool) {
    item.active = item.chartType[0];
  } else {
    item.active = "";
  }
};
const propertyList = ref([]) as any;
const GetAllPropertyFun = async () => {
  let stationName = "";
  const station = OPList.value.find(
    (e: any) => e.id === params.value.stationId
  );
  if (station) {
    stationName = station.stationName;
  } else {
    store.info("请线选择OP属性"); // 错误提示
    return false;
  }
  const res: any = await GetAllProperty({ stationName: stationName });
  if (res.result.isSuccess) {
    let list = res.result.result.map((e: any) => {
      return { value: e.deviceCode, label: e.deviceCode, datas: e.datas };
    });

    let list2: any = echarlsList.value;
    list2.forEach((listItem: any) => {
      const [prefix, suffix] = listItem.propertyName.split("-");
      const matchingItem = list.find((item: any) => prefix === item.value);
      if (matchingItem) {
        const matchingData = matchingItem.datas.find(
          (data: any) => data.propertyName === suffix
        );
        if (matchingData) {
          matchingData.sort = listItem.sort;
          matchingData.checked = true;
          matchingData.active = listItem.chartType;
        }
      }
    });
    list = list.map((m: any) => {
      m.datas = m.datas.map((e: any) => {
        if (!e.sort) {
          e.sort = 0;
        }
        return e;
      });
      return m;
    });
    propertyList.value = list;
    openMoreWin();
  } else {
    store.error(res.result.message); // 错误提示
  }
};
//获取所有站点配置
const GetAllStationInfoFun = async () => {
  const res: any = await GetAllStationInfo({
    modelId: params.value.modelId,
  });
  if (res.result.isSuccess) {
    params.value.stationId = null;
    OPList.value = res.result.result.map((e: any) => {
      e.value = e.id;
      e.label = e.stationName;
      return e;
    });
    if (OPList.value.length > 0) {
      params.value.stationId = OPList.value[0].id;
      GetStationConfigurationFun();
    }
  } else {
    store.error(res.result.message); // 错误提示
  }
};

//获取产品名称列表
const GetProductSNListFun = async () => {
  const res: any = await GetProductSNList({
    productName: params.value.productName,
  });
  if (res.result.isSuccess) {
    params.value.productSN = null;
    productSNList.value = res.result.result.map((e: any) => {
      return { value: e, label: e, id: e };
    });
  } else {
    store.error(res.result.message); // 错误提示
  }
};
//获取产品名称列表
const GetProductNameListFun = async () => {
  const res: any = await GetProductNameList({});
  if (res.result.isSuccess) {
    productNameList.value = res.result.result.map((e: any) => {
      return { value: e, label: e, id: e };
    });
  } else {
    store.error(res.result.message); // 错误提示
  }
};

//获取初始化数据1
const GetPageA2NameFun = async () => {
  const res: any = await GetPageA2Name(params.value);
  if (res.result.isSuccess) {
    stationList.value = res.result.result.map((e: any) => {
      e.value = e.modelId;
      e.label = e.name;
      return e;
    });
  } else {
    store.error(res.result.message); // 错误提示
  }
};
GetPageA2NameFun();
GetProductNameListFun();
</script>

<style lang="less" scoped>
.b1-page {
  width: 100vw;
  height: calc(100vh - 2.4rem);
  overflow: hidden;
  background: #ffffff;

  .tool-view {
    position: relative;
    z-index: 10;
    padding: 0.3rem 0;
    transform: scale(0.7);
    transform-origin: top left;
    width: 100%;
    display: flex;
    align-items: center;
    background: #e3e3e3;
    backdrop-filter: blur(32px);

    .tool-l {
      display: flex;
      align-items: center;
      .pcker-view {
        position: relative;
        .mask {
          position: absolute;
          z-index: 1;
          width: 100%;
          height: 100%;
        }
      }
      span {
        font-size: 1rem;
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        color: #1c262d;
        margin: 0 0.5rem;
      }
    }
  }
  .table-view {
    margin-top: -0.8rem;
    height: calc(100% - 2.4rem);
    display: flex;
    .table-left {
      width: 10rem;
      padding: 0.5rem;
      .title {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 7.625rem;
        height: 1.5rem;
        background: #5d6a73;
        box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.08),
          0px 1px 3px 0px rgba(25, 15, 15, 0.07),
          0px 3px 5px 0px rgba(0, 0, 0, 0.05);
        font-size: 0.6875em;
        font-family: SourceHanSansCN, SourceHanSansCN;
        font-weight: bold;
        color: #ffffff;
        margin-bottom: 1rem;
      }
      :deep(.n-step-indicator) {
        width: 1.4rem;
        height: 1.4rem;
        margin: 0.2rem;
      }
      :deep(.n-step-content-header__title) {
        font-size: 0.75rem;
      }
    }
    .table-right {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      align-content: flex-start;
      .load {
        height: 50vh;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .card {
        position: relative;
        width: 50%;
        height: 60vh;
        padding: 0.5rem;
        border-bottom: 0.825rem solid #dbdbdb;
        .title {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 7.625rem;
          height: 1.5rem;
          background: #5d6a73;
          box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.08),
            0px 1px 3px 0px rgba(25, 15, 15, 0.07),
            0px 3px 5px 0px rgba(0, 0, 0, 0.05);
          font-size: 0.6875em;
          font-family: SourceHanSansCN, SourceHanSansCN;
          font-weight: bold;
          color: #ffffff;
          margin-bottom: 0.5rem;
        }
        .card-content {
          width: 100%;
          height: calc(100% - 2rem);
        }
      }
    }
  }
}
.step-desc {
  font-size: 0.56rem;
  font-family: SourceHanSansCN, SourceHanSansCN;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  line-height: 0.8rem;
  text-align: left;
}
:deep(.n-step-indicator) {
  background: #02a8ae;
  box-shadow: 0 0 0 1px #02a8ae;
}
:deep(.n-step-splitor) {
  background: rgba(0, 0, 0, 0.15);
}
:deep(.n-step-content-header) {
  margin-top: 0 !important;
}
:deep(.n-step-content-header__title) {
  font-size: 22px;
  font-family: SourceHanSansCN, SourceHanSansCN;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.85);
  line-height: 33px;
}
// :deep(.n-date-panel) {
//   transform: scale(0.95);
//   transform-origin: top left;
// }
</style>
